package com.oil.petro.eshop.utils;

import com.oil.petro.eshop.bean.BasicBean;
import com.oil.petro.eshop.bean.Page;
import com.oil.petro.eshop.service.ProductService;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;

public class PaginationUtil {

    private final static int LINKS = 4;
//
//    public List<Page> getPageList(int amount, int pageNumber, int idf, ProductService productService) {
//
//        List<Page> pageList = new ArrayList<Page>();
//        Page page;
//
//        if (pageNumber > 0 && productService.isNextProductListPage(amount, pageNumber)) {
//            page = new Page("&laquo;", "list.htm?idf=" + idf + "&page=" + (pageNumber));
//            pageList.add(page);
//
//        }
//
//        for (int i = pageNumber; i < LINKS + pageNumber; i++) {
//            System.out.println("numer " + i);
//
//            if (pageNumber > 0 && i < LINKS + pageNumber - 3 && productService.isNextProductListPage(amount, i)) {
//                page = new Page(String.valueOf(i + 1), "list.htm?idf=" + idf + "&page=" + (i + 1));
//                pageList.add(page);
//            } else if (pageNumber > 0 && i < LINKS + pageNumber - 2 && productService.isNextProductListPage(amount, i)) {
//                page = new Page(String.valueOf(i + 1), "list.htm?idf=" + idf + "&page=" + (i + 1));
//                pageList.add(page);
//            } else if (pageNumber > 0 && i < LINKS + pageNumber - 1 && productService.isNextProductListPage(amount, i)) {
//                page = new Page(String.valueOf(i + 1), "list.htm?idf=" + idf + "&page=" + (i + 1));
//                pageList.add(page);
//            } else if (pageNumber == 0 && i < LINKS + pageNumber - 1 && productService.isNextProductListPage(amount, i)) {
//                page = new Page(String.valueOf(i + 1), "list.htm?idf=" + idf + "&page=" + (i + 1));
//                pageList.add(page);
//            } else if (i == LINKS + pageNumber - 1 && productService.isNextProductListPage(amount, i)) {
//                page = new Page("&raquo;", "list.htm?idf=" + idf + "&page=" + (i + 1));
//                pageList.add(page);
//            }
//
//        }
//        return pageList;
//    }

    public List<Page> getPageList(int amount, int pageNumber, ProductService productService) {

        List<Page> pageList = new ArrayList<Page>();
        Page page;

        pageList = getLinkList(amount, pageNumber, productService);

        if (pageNumber == 1) {
            for (int j = pageNumber + 1; j <= pageNumber + 3; j++) {
                if (pageNumber + 3 == j && productService.isNextProductListPage(amount, j)) {
                    page = new Page("&raquo;", "list.htm?page=" + j);
                    pageList.add(page);
                } else if (pageNumber + 1 == j && productService.isNextProductListPage(amount, j)) {
                    page = new Page(String.valueOf(j), "list.htm?page=" + j);
                    pageList.add(page);
                } else if (pageNumber + 2 == j && productService.isNextProductListPage(amount, j)) {
                    page = new Page(String.valueOf(j), "list.htm?page=" + j);
                    pageList.add(page);
                }
            }
        } else {
            for (int j = pageNumber + 1; j <= pageNumber + 2; j++) {
                if (pageNumber + 2 == j && productService.isNextProductListPage(amount, j)) {
                    page = new Page("&raquo;", "list.htm?page=" + j);
                    pageList.add(page);
                } else if (pageNumber + 1 == j && !productService.isNextProductListPage(amount, j)) {
                    pageList = getLinkList(amount, pageNumber - 1, productService);
                    page = new Page(String.valueOf(j - 1), "list.htm?page=" + (j - 1));
                    pageList.add(page);
                } else if (pageNumber + 1 == j) {
                    page = new Page(String.valueOf(j), "list.htm?page=" + (j));
                    pageList.add(page);
                }
            }
        }

        Collections.sort(pageList);
        setActiveLink(pageList, pageNumber);
        return pageList;
    }

    List<Page> getLinkList(int amount, int pageNumber, ProductService productService) {
        List<Page> pageList = new ArrayList<Page>();
        Page page;

        for (int i = pageNumber; i > pageNumber - 2; i--) {
            if (i == 1) {
                page = new Page(String.valueOf(i), "list.htm?page=" + i);
                pageList.add(page);
            } else if (pageNumber - i == 1 && pageNumber > 2) {
                page = new Page("&laquo;", "list.htm?page=" + (pageNumber - 2));
                pageList.add(page);
            }
            if (i > 1) {
                page = new Page(String.valueOf(i), "list.htm?page=" + i);
                pageList.add(page);
            }
        }
        return pageList;

    }

    public List<Page> getSearchPageList(BasicBean basicBean, int amount, int pageNumber, int idf, ProductService productService) {

        List<Page> pageList = new ArrayList<Page>();
        Page page;

        pageList = getSearchLinkList(basicBean, amount, pageNumber, idf, productService);

        if (pageNumber == 1) {
            for (int j = pageNumber + 1; j <= pageNumber + 3; j++) {
                if (pageNumber + 3 == j && productService.isNextSearchProductListPage(basicBean, amount, j)) {
                    page = new Page("&raquo;", "list.htm?idf=" + idf + "&page=" + j
                            + "&category=" + basicBean.getCategory() + "&manufacturer=" + basicBean.getManufacturer()
                            + "&cost=" + basicBean.getCost());
                    pageList.add(page);
                } else if (pageNumber + 1 == j && productService.isNextSearchProductListPage(basicBean, amount, j)) {
                    page = new Page(String.valueOf(j), "list.htm?idf=" + idf + "&page=" + j
                            + "&category=" + basicBean.getCategory() + "&manufacturer=" + basicBean.getManufacturer()
                            + "&cost=" + basicBean.getCost());
                    pageList.add(page);
                } else if (pageNumber + 2 == j && productService.isNextSearchProductListPage(basicBean, amount, j)) {
                    page = new Page(String.valueOf(j), "list.htm?idf=" + idf + "&page=" + j
                            + "&category=" + basicBean.getCategory() + "&manufacturer=" + basicBean.getManufacturer()
                            + "&cost=" + basicBean.getCost());
                    pageList.add(page);
                }
            }
        } else {
            for (int j = pageNumber + 1; j <= pageNumber + 2; j++) {
                if (pageNumber + 2 == j && productService.isNextSearchProductListPage(basicBean, amount, j)) {
                    page = new Page("&raquo;", "list.htm?idf=" + idf + "&page=" + j
                            + "&category=" + basicBean.getCategory() + "&manufacturer=" + basicBean.getManufacturer()
                            + "&cost=" + basicBean.getCost());
                    pageList.add(page);
                } else if (pageNumber + 1 == j && !productService.isNextSearchProductListPage(basicBean, amount, j)) {
                    pageList = getSearchLinkList(basicBean, amount, pageNumber - 1, idf, productService);
                    page = new Page(String.valueOf(j - 1), "list.htm?idf=" + idf + "&page=" + (j - 1)
                            + "&category=" + basicBean.getCategory() + "&manufacturer=" + basicBean.getManufacturer()
                            + "&cost=" + basicBean.getCost());
                    pageList.add(page);
                } else if (pageNumber + 1 == j) {
                    page = new Page(String.valueOf(j), "list.htm?idf=" + idf + "&page=" + (j)
                            + "&category=" + basicBean.getCategory() + "&manufacturer=" + basicBean.getManufacturer()
                            + "&cost=" + basicBean.getCost());
                    pageList.add(page);
                }
            }
        }

        Collections.sort(pageList);
        setActiveLink(pageList, pageNumber);
        return pageList;
    }

    List<Page> getSearchLinkList(BasicBean basicBean, int amount, int pageNumber, int idf, ProductService productService) {
        List<Page> pageList = new ArrayList<Page>();
        Page page;

        for (int i = pageNumber; i > pageNumber - 2; i--) {
            if (i == 1) {
                page = new Page(String.valueOf(i), "list.htm?idf=" + idf + "&page=" + i
                        + "&category=" + basicBean.getCategory() + "&manufacturer=" + basicBean.getManufacturer()
                        + "&cost=" + basicBean.getCost());
                pageList.add(page);
            } else if (pageNumber - i == 1 && pageNumber > 2) {
                page = new Page("&laquo;", "list.htm?idf=" + idf + "&page=" + (pageNumber - 2)
                        + "&category=" + basicBean.getCategory() + "&manufacturer=" + basicBean.getManufacturer()
                        + "&cost=" + basicBean.getCost());
                pageList.add(page);
            }
            if (i > 1) {
                page = new Page(String.valueOf(i), "list.htm?idf=" + idf + "&page=" + i
                        + "&category=" + basicBean.getCategory() + "&manufacturer=" + basicBean.getManufacturer()
                        + "&cost=" + basicBean.getCost());
                pageList.add(page);
            }
        }
        return pageList;

    }

    private void setActiveLink(List<Page> pageList, int pageNumber) {
        for (Page page : pageList) {
            if (page.getNumber().equals(pageNumber + "")) {

                System.out.println("aaasssddd");
                page.setStyle("active");

            }

        }

    }

//     public List<Page> getPageList2(int amount, int pageNumber, int idf, ProductService productService) {
//     
//         for(int i=pageNumber;i>=pageNumber-2;i--){
//         System.out.println("iiii "+i);
//         
//        
//         if(pageNumber==1){
//         przypisz 1
//         
//         } else if (pageNumber>2){
//         cala reszta
//         }
//         
//         
//         }
//         
//         
//         for(int j = pageNumber+1;j<=pageNumber+2;j++ ){
//         System.out.println("jjjjj "+j);
//             
//         }
//         
//         
//     
//     
//         
//         
//     
//     }
//    public List<Page> getSearchPageList(BasicBean basicBean, int amount, int pageNumber, int idf, ProductService productService) {
//
//        List<Page> pageList = new ArrayList<Page>();
//        Page page;
//
//        if (pageNumber > 0 && productService.isNextSearchProductListPage(basicBean, amount, pageNumber)) {
//            page = new Page("&laquo;", "list.htm?idf=" + idf + "&page=" + (pageNumber)
//                    + "&category=" + basicBean.getCategory() + "&manufacturer=" + basicBean.getManufacturer()
//                    + "&cost=" + basicBean.getCost());
//
//            pageList.add(page);
//
//        }
//
//        for (int i = pageNumber; i < LINKS + pageNumber; i++) {
//            System.out.println("numer " + i);
//
//            if (pageNumber > 0 && i < LINKS + pageNumber - 1 && productService.isNextSearchProductListPage(basicBean, amount, i)) {
//                page = new Page(String.valueOf(i + 1), "list.htm?idf=" + idf + "&page=" + (i + 1)
//                        + "&category=" + basicBean.getCategory() + "&manufacturer=" + basicBean.getManufacturer()
//                        + "&cost=" + basicBean.getCost());
//                pageList.add(page);
//            }
//
//            if (pageNumber == 0 && i < LINKS + pageNumber - 1 && productService.isNextSearchProductListPage(basicBean, amount, i)) {
//                page = new Page(String.valueOf(i + 1), "list.htm?idf=" + idf + "&page=" + (i + 1)
//                        + "&category=" + basicBean.getCategory() + "&manufacturer=" + basicBean.getManufacturer()
//                        + "&cost=" + basicBean.getCost());
//                pageList.add(page);
//            }
//
//            if (i == LINKS + pageNumber - 1 && productService.isNextSearchProductListPage(basicBean, amount, i)) {
//                page = new Page("&raquo;", "list.htm?idf=" + idf + "&page=" + (i + 1)
//                        + "&category=" + basicBean.getCategory() + "&manufacturer=" + basicBean.getManufacturer()
//                        + "&cost=" + basicBean.getCost());
//                pageList.add(page);
//            }
//
//        }
//        return pageList;
//    }


 public List<Page> getPageList(String url,int amount, int pageNumber, ProductService productService) {

        List<Page> pageList = new ArrayList<Page>();
        Page page;

        pageList = getLinkList(url,amount, pageNumber, productService);

        if (pageNumber == 1) {
            for (int j = pageNumber + 1; j <= pageNumber + 3; j++) {
                if (pageNumber + 3 == j && productService.isNextProductListPage(amount, j)) {
                    page = new Page("&raquo;", url+".htm?page=" + j);
                    pageList.add(page);
                } else if (pageNumber + 1 == j && productService.isNextProductListPage(amount, j)) {
                    page = new Page(String.valueOf(j), url+".htm?page=" + j);
                    pageList.add(page);
                } else if (pageNumber + 2 == j && productService.isNextProductListPage(amount, j)) {
                    page = new Page(String.valueOf(j), url+".htm?page=" + j);
                    pageList.add(page);
                }
            }
        } else {
            for (int j = pageNumber + 1; j <= pageNumber + 2; j++) {
                if (pageNumber + 2 == j && productService.isNextProductListPage(amount, j)) {
                    page = new Page("&raquo;", url+".htm?page=" + j);
                    pageList.add(page);
                } else if (pageNumber + 1 == j && !productService.isNextProductListPage(amount, j)) {
                    pageList = getLinkList(url,amount, pageNumber - 1, productService);
                    page = new Page(String.valueOf(j - 1), url+".htm?page=" + (j - 1));
                    pageList.add(page);
                } else if (pageNumber + 1 == j) {
                    page = new Page(String.valueOf(j), url+".htm?page=" + (j));
                    pageList.add(page);
                }
            }
        }

        Collections.sort(pageList);
        setActiveLink(pageList, pageNumber);
        return pageList;
    }

    List<Page> getLinkList(String url,int amount, int pageNumber, ProductService productService) {
        List<Page> pageList = new ArrayList<Page>();
        Page page;

        for (int i = pageNumber; i > pageNumber - 2; i--) {
            if (i == 1) {
                page = new Page(String.valueOf(i), url+".htm?page=" + i);
                pageList.add(page);
            } else if (pageNumber - i == 1 && pageNumber > 2) {
                page = new Page("&laquo;", url+".htm?page=" + (pageNumber - 2));
                pageList.add(page);
            }
            if (i > 1) {
                page = new Page(String.valueOf(i), url+".htm?page=" + i);
                pageList.add(page);
            }
        }
        return pageList;

    }


}
